package de.duester.genealogy.model.service;

import java.util.ArrayList;
import java.util.List;

import de.duester.genealogy.Utility;
import de.duester.genealogy.model.Document;
import de.duester.genealogy.model.DocumentPlace;
import de.duester.genealogy.model.Event;
import de.duester.genealogy.model.EventPlace;
import de.duester.genealogy.model.Place;
import de.duester.genealogy.model.PlaceCode;
import de.duester.genealogy.model.dao.PlaceCodeDao.Properties;
import de.greenrobot.dao.DaoException;

public class PlaceService {

	public static List<Event> getEvents(Place place) {
		List<Event> list = new ArrayList<Event>();
		try {
			for (EventPlace ep : place.getEventPlaces())
				if (!list.contains(ep.getEvent()))
					list.add(ep.getEvent());
		} catch (DaoException e) {
		}
		return list;
	}

	public static List<Event> getEvents(Place place, String typeCode) {
		List<Event> list = new ArrayList<Event>();
		List<Event> events = getEvents(place);
		for (Event event : events)
			if (event.getTypeCode().equals(typeCode))
				list.add(event);
		return list;
	}

	public static List<Document> getDocuments(Place place) {
		List<Document> list = new ArrayList<Document>();
		try {
			for (DocumentPlace dp : place.getDocumentPlaces())
				if (!list.contains(dp.getDocument()))
					list.add(dp.getDocument());
		} catch (DaoException e) {
		}
		return list;
	}

	public static List<Document> getDocuments(Place place, int type) {
		List<Document> list = new ArrayList<Document>();
		List<Document> documents = getDocuments(place);
		for (Document document : documents)
			if (document.getType().equals(type))
				list.add(document);
		return list;
	}

	public static String getName(String code) {
		List<PlaceCode> pc = Utility.getDaoSession().getPlaceCodeDao().queryBuilder()
				.where(Properties.Code.eq(code)).list();
		if (pc.size() > 0)
			return pc.get(0).getName();
		else
			return null;
	}
}
